<template>
  <div class="cmtItem">
    <!-- 用户头像 -->
    <div class="cover">
      <img :src="$axios.defaults.baseURL+commentData.account.defaultAvatar" alt />
    </div>
    <!-- 评论内容 -->
    <div class="content">
      <!-- 最新评论 -->
      <div class="userSay">
        <span>{{commentData.account.nickname}}：</span>
        {{commentData.content}}
      </div>
      <el-row>
        <el-col v-for="item in commentData.pics" :key="item.id" :span="3" class="pic">
          <img @click="showImg" :src="$axios.defaults.baseURL + item.url" alt />
        </el-col>
        <el-dialog :visible.sync="dialogVisible" size="tiny">
          <img width="100%" :src="dialogImageUrl" alt />
        </el-dialog>
      </el-row>
      <!-- 评论回复 -->
      <div class="func">
        <div class="time">{{time}}</div>
        <a href="javascript:;" class="reply" @click="replyCmt">回复</a>
      </div>
      <!-- 评论子组件 -->
      <commentParent
        v-if="commentData.parent"
        :parentData="commentData.parent"
        @parentCallReply="parentCallReply"
      />
    </div>
  </div>
</template>

<script>
import commentParent from "@/components/raider/commentParent";
import moment from "moment";
export default {
  components: {
    commentParent,
  },
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
    };
  },
  props: ["commentData"],
  computed: {
    time() {
      return moment(this.commentData.created_at).format("YYYY-MM-DD HH:mm");
    },
  },
  methods: {
    showImg(e) {
      this.dialogImageUrl = e.path[0].src;
      this.dialogVisible = true;
    },
    //点击回复跳到文本框
    returnTop() {
      document.querySelector("#dingbu").scrollIntoView(true);
    },
    //主评论回复
    replyCmt() {
      this.returnTop();
      this.$emit("userInfo", {
        id: this.commentData.id,
        nickname: this.commentData.account.nickname,
      });
    },
    // 出口，接收参数传递到最外层
    parentCallReply(obj) {
      this.$emit("userInfo", obj);
    },
  },
};
</script>

<style lang="less" scoped>
.cmtItem {
  display: flex;
  padding: 20px;
  .cover {
    margin-right: 10px;
    img {
      width: 30px;
      height: 30px;
    }
  }
  .content {
    flex: 1;
    font-size: 14px;
    color: #333;
    .userSay {
      span {
        color: #ffa500;
      }
    }
    .pic {
      margin: 5px;
      img {
        width: 100%;
        height: 100%;
        display: block;
        object-fit: cover;
        cursor: pointer;
      }
    }
    .func {
      margin: 5px 0;
      display: flex;
      justify-content: space-between;
      a {
        &:hover {
          text-decoration: underline;
          color: #ffa500;
        }
      }
    }
  }
}
</style>